const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pcsdata: [],
    areadata: [],
    hyBigTypeData: [],
    hySmTypeData: [],
    deptAddress: "",
    deptName: "",
    deptPosition: "",
    idNo: "",
    legalName: "",
    mgrPCS: "",
    mgrPCSCode: "",
    owerArea: "",
    owerAreaCode: "",
    sectorType: "",
    sectorTypeCode: "",
    sectorXType: "",
    sectorXTypeCode: "",
    telephone: "",
    unitCode: "",
    modalHidden: true
  },
  //表单提交
  formSubmit(e) {
    const { telephone, idNo, deptName, deptAddress, deptPosition, legalName } = e.detail.value;
    if (!telephone || !idNo || !deptName
      || !deptAddress || !deptPosition || !legalName) {
      wx.showToast({
        title: '请填写必填项！！！',
        duration: 2000,
        icon: 'none', //图标有success、error、loading、none四种
        mask: true //显示透明蒙层 防止触摸穿透
      })
      return;
    }
    if (!(/^1[34578]\d{9}$/.test(telephone))) {
      wx.showToast({
        title: '联系电话填写错误！！！',
        duration: 2000,
        icon: 'none', //图标有success、error、loading、none四种
        mask: true //显示透明蒙层 防止触摸穿透
      })
      return;
    }
    if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(idNo))) {
      wx.showToast({
        title: '身份证填写错误！！！',
        duration: 2000,
        icon: 'none', //图标有success、error、loading、none四种
        mask: true //显示透明蒙层 防止触摸穿透
      })
      return;
    }
    this.setData({
      deptName: deptName,
      deptAddress: deptAddress,
      deptPosition: deptPosition,
      legalName: legalName,
      idNo: idNo,
      telephone: telephone,
      modalHidden: false
    });
  },
  delInfoReport() {
    wx.request({
      url: app.globalData.delInfoReportUrl,
      data: {
        unitCode: this.data.unitCode
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (data2) {
        if (data2.statusCode == 200) {
          wx.showToast({
            icon: "none",
            title: '注销设备成功！',
          })
          setTimeout(function () {
            // wx.navigateTo({
            //   url: '/pages/delpickunit/pickunit'
            // })
            wx.navigateBack({
              delta: 1
            });
          }, 2000)
        } else {
          wx.showToast({
            icon: "none",
            title: '服务器异常，请稍后再试！',
          })
        }
      },
      fail: function (err) {
        console.log(err);
        wx.showToast({
          icon: "none",
          title: '注销设备失败，请重试！',
        })
      }
    })
  },
  //模态框取消
  modalCancel() {
    wx.showToast({
      title: '取消提交',
      icon: 'none'
    })
    this.setData({
      modalHidden: true,
    })
  },
  //模态框确定
  modalConfirm() {
    this.delInfoReport();
    // wx.showToast({
    //   title: '提交成功',
    //   icon: 'success'
    // })
    this.setData({
      modalHidden: true
    })
  },
  onChange(e) {

  },
  onLoad: function (options) {
    // console.log("当前页面参数", options);
    this.setData({
      unitCode: options.code
    });
    this.getInfoReportByNo();
  },

  getInfoReportByNo() {
    var that = this;
    wx.request({
      url: app.globalData.getInfoReportByNo,
      data: { unitCode: that.data.unitCode },
      method: "GET",
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.data.status === "200") {
          const dictData = res.data.data || {};
          that.setData({
            deptAddress: dictData.unit_address,
            deptName: dictData.unit_name,
            deptPosition: dictData.unit_position,
            idNo: dictData.id_no,
            legalName: dictData.legal_name,
            mgrPCS: dictData.mgr_pcs,
            mgrPCSCode: dictData.mgr_pcs_code,
            owerArea: dictData.owner_area,
            owerAreaCode: dictData.owner_area_code,
            sectorType: dictData.sector_type,
            sectorTypeCode: dictData.sector_type_code,
            sectorXType: dictData.sector_xtype,
            sectorXTypeCode: dictData.sector_xtype_code,
            telephone: dictData.telephone
          });
          // console.log(that.data)
        }
      }
    })
  }
})